1. /* slflsmul.cpp by K.Tsuru */
  2. // function ID = 212 DRADIX(for BRADIX use IsMult )
  3. /************************************************************
  4. SLong class
  5. m*n
  6. It multiplies m by a small number n where 0 <= n < ULONG_MAX.
  7. **************************************************************/
  8. #ifndef SN_H
  9. #include "sn.h"
  10. #endif
  11. SLong LsMult(const SLong& m, ulong n){
  12. const ulong mt = m.SlOpMaxValue();
  13. if(n > mt){
  14. SLong s(m.Type(), 0);
  15. s = n;
  16. return NLLMult(m, s);
  17. // m.SetError(m.OUT_OF_RANGE,"LsMult", 212);
  18. }
  19. if(n == 1uL) return m;
  20. SLong result(m.Type(), 0);
  21. uint rh = m.aHead, rt = m.aTail, i;
  22. if( !n || !m.Sign(212) ){
  23. result.SetZero();
  24. return result;
  25. }
  26. result.valloc(rh+1, -1);
  27. if(rt) result.figure.clear(0, rt-1);
  28. fType* rv = result.figure.Elements();
  29. const fType* mv = m.ReadFigures();
  30. ulong w, u = 0, rdx = m.Radix();
  31. for(i = rt; i <= rh ; i++){
  32. w = (ulong)mv[i]*n + u;
  33. u = w/rdx; //carry
  34. rv[i] = fType(w - u*rdx); //faster than rv[i] = fType(w%rdx);
  35. }
  36. if(u){//carry to the top figure
  37. if(u < rdx){//lass than radix
  38. rh++;
  39. result.Reserve(rh);
  40. result.figure[rh] = (fType)u;
  41. rv = result.figure.Elements();
  42. } else {//greater than radix
  43. //it normalizes the carry and copy to the top position.
  44. fType temp[5];
  45. i = 0;
  46. while(u){
  47. temp[i] = fType(u%rdx);
  48. u /= rdx; i++;
  49. }
  50. result.Reserve(rh+i); // result.size() >= rh+i+1
  51. rv = result.figure.Elements();
  52. memcpy( (rv+rh+1), temp, i*sizeof(fType));
  53. rh += i;
  54. }
  55. }
  56. //It gets the figure position.
  57. result.aHead = rh;
  58. result.figure.clear(rh+1);
  59. while(!rv[rt]) rt++;
  60. result.aTail = rt;
  61. result.SetSign( m.Sign() );
  62. return result;
  63. }

slflsmul.cpp : last modifiled at 2017/03/13 14:32:00(1,735 bytes)
created at 2017/10/07 10:26:49
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).